{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "ff80ea5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import netCDF4 as nc  # 用于读取netcdf文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "c8eb5ce4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<class 'netCDF4.Dataset'>\n",
       "root group (NETCDF3_CLASSIC data model, file format NETCDF3):\n",
       "    dimensions(sizes): time(112), latitude(181), longitude(360)\n",
       "    variables(dimensions): float32 t2m(time, latitude, longitude), int32 time(time), float32 latitude(latitude), float32 longitude(longitude)\n",
       "    groups: "
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fpath = './201902_t2m_float.nc'\n",
    "\n",
    "# 读取netcdf文件\n",
    "f = nc.Dataset(fpath,'r')\n",
    "\n",
    "# 预览文件信息\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "1a07659b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['t2m', 'time', 'latitude', 'longitude'])\n"
     ]
    }
   ],
   "source": [
    "# 查看netcdf文件里有哪些变量\n",
    "print(f.variables.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "3112f82e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'netCDF4.Variable'>\n",
      "float32 t2m(time, latitude, longitude)\n",
      "    units: K\n",
      "    long_name: 2 metre temperature\n",
      "    _FillValue_original: -32767\n",
      "    _FillValue: -32767.0\n",
      "    missing_value_original: -32767\n",
      "    missing_value: -32767.0\n",
      "unlimited dimensions: \n",
      "current shape = (112, 181, 360)\n",
      "filling on\n",
      "------------------------------\n",
      "<class 'netCDF4.Variable'>\n",
      "int32 time(time)\n",
      "    calendar: gregorian\n",
      "    long_name: time\n",
      "    units: hours since 1900-01-01 00:00:00.0\n",
      "unlimited dimensions: \n",
      "current shape = (112,)\n",
      "filling on, default _FillValue of -2147483647 used\n",
      "------------------------------\n",
      "<class 'netCDF4.Variable'>\n",
      "float32 latitude(latitude)\n",
      "    long_name: latitude\n",
      "    units: degrees_north\n",
      "unlimited dimensions: \n",
      "current shape = (181,)\n",
      "filling on, default _FillValue of 9.969209968386869e+36 used\n",
      "------------------------------\n",
      "<class 'netCDF4.Variable'>\n",
      "float32 longitude(longitude)\n",
      "    long_name: longitude\n",
      "    units: degrees_east\n",
      "unlimited dimensions: \n",
      "current shape = (360,)\n",
      "filling on, default _FillValue of 9.969209968386869e+36 used\n",
      "------------------------------\n"
     ]
    }
   ],
   "source": [
    "# 查看变量PREC详细信息\n",
    "for key in f.variables.keys():\n",
    "    print(f.variables[key])\n",
    "    print('---' * 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "8cc0cd2a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1043880 1043886 1043892 1043898 1043904 1043910 1043916 1043922 1043928\n",
      " 1043934 1043940 1043946 1043952 1043958 1043964 1043970 1043976 1043982\n",
      " 1043988 1043994 1044000 1044006 1044012 1044018 1044024 1044030 1044036\n",
      " 1044042 1044048 1044054 1044060 1044066 1044072 1044078 1044084 1044090\n",
      " 1044096 1044102 1044108 1044114 1044120 1044126 1044132 1044138 1044144\n",
      " 1044150 1044156 1044162 1044168 1044174 1044180 1044186 1044192 1044198\n",
      " 1044204 1044210 1044216 1044222 1044228 1044234 1044240 1044246 1044252\n",
      " 1044258 1044264 1044270 1044276 1044282 1044288 1044294 1044300 1044306\n",
      " 1044312 1044318 1044324 1044330 1044336 1044342 1044348 1044354 1044360\n",
      " 1044366 1044372 1044378 1044384 1044390 1044396 1044402 1044408 1044414\n",
      " 1044420 1044426 1044432 1044438 1044444 1044450 1044456 1044462 1044468\n",
      " 1044474 1044480 1044486 1044492 1044498 1044504 1044510 1044516 1044522\n",
      " 1044528 1044534 1044540 1044546]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "masked_array(data=[cftime.DatetimeGregorian(2019, 2, 1, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 1, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 1, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 1, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 2, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 2, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 2, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 2, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 3, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 3, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 3, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 3, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 4, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 4, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 4, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 4, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 5, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 5, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 5, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 5, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 6, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 6, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 6, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 6, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 7, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 7, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 7, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 7, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 8, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 8, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 8, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 8, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 9, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 9, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 9, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 9, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 10, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 10, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 10, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 10, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 11, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 11, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 11, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 11, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 12, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 12, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 12, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 12, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 13, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 13, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 13, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 13, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 14, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 14, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 14, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 14, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 15, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 15, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 15, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 15, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 16, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 16, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 16, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 16, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 17, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 17, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 17, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 17, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 18, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 18, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 18, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 18, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 19, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 19, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 19, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 19, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 20, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 20, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 20, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 20, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 21, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 21, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 21, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 21, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 22, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 22, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 22, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 22, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 23, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 23, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 23, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 23, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 24, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 24, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 24, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 24, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 25, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 25, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 25, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 25, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 26, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 26, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 26, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 26, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 27, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 27, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 27, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 27, 18, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 28, 0, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 28, 6, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 28, 12, 0, 0, 0, has_year_zero=False),\n",
       "                   cftime.DatetimeGregorian(2019, 2, 28, 18, 0, 0, 0, has_year_zero=False)],\n",
       "             mask=False,\n",
       "       fill_value=np.str_('?'),\n",
       "            dtype=object)"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取变量时间\n",
    "time = f.variables['time'][:]\n",
    "print(time)\n",
    "# 查看时间信息\n",
    "nc.num2date(time, f.variables['time'].units)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "3b8f1dc2",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Desktop-A10\\AppData\\Local\\Temp\\ipykernel_36792\\2371240295.py:2: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead.\n",
      "  my_time = pd.date_range('2019-02-01 00:00:00','2019-02-28 18:00:00',freq='6H')\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2019-02-01 00:00:00', '2019-02-01 06:00:00',\n",
       "               '2019-02-01 12:00:00', '2019-02-01 18:00:00',\n",
       "               '2019-02-02 00:00:00', '2019-02-02 06:00:00',\n",
       "               '2019-02-02 12:00:00', '2019-02-02 18:00:00',\n",
       "               '2019-02-03 00:00:00', '2019-02-03 06:00:00',\n",
       "               ...\n",
       "               '2019-02-26 12:00:00', '2019-02-26 18:00:00',\n",
       "               '2019-02-27 00:00:00', '2019-02-27 06:00:00',\n",
       "               '2019-02-27 12:00:00', '2019-02-27 18:00:00',\n",
       "               '2019-02-28 00:00:00', '2019-02-28 06:00:00',\n",
       "               '2019-02-28 12:00:00', '2019-02-28 18:00:00'],\n",
       "              dtype='datetime64[ns]', length=112, freq='6h')"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 新建时间变量\n",
    "my_time = pd.date_range('2019-02-01 00:00:00','2019-02-28 18:00:00',freq='6H')\n",
    "my_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "73050cc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取经纬度变量\n",
    "lat = f.variables['latitude'][:]\n",
    "lon = f.variables['longitude'][:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "12c24cb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置读取范围\n",
    "\n",
    "# 创建时间戳\n",
    "start_date = pd.Timestamp('2000-01-01') # 起始时间\n",
    "end_date = pd.Timestamp('2011-12-31') # \n",
    "\n",
    "# 读取区域\n",
    "latS = 30\n",
    "latN = 30\n",
    "lonL = 104\n",
    "lonR = 104"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "6117c3c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_nearest(array,value):\n",
    "    # 转换为array数组\n",
    "    arr = np.asarray(array)\n",
    "    # 查找最小值的索引\n",
    "    idx = np.abs(arr - value).argmin()\n",
    "    return idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "88d06bc1",
   "metadata": {},
   "outputs": [],
   "source": [
    "jds = find_nearest(lat, latS)\n",
    "jde = find_nearest(lat, latN)\n",
    "ids = find_nearest(lon, lonL)\n",
    "ide = find_nearest(lon, lonR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "ad444fb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(112,)\n"
     ]
    }
   ],
   "source": [
    "# 读取指定范围数据\n",
    "t2m = f.variables['t2m'][:,jde,ids]\n",
    "print(t2m.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "082cd793",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建时间序列\n",
    "aave_prec = pd.DataFrame({'date':my_time,'temp':t2m})\n",
    "\n",
    "# 保存为csv文件\n",
    "fopath = './成都气温数据.csv'\n",
    "aave_prec.to_csv(fopath,index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
